/* eslint-disable */
<template>

  <div class="class-table">

    <div class="search-term">
      <el-form ref="elForm" :model="formData" size="medium" label-width="100px">
        <el-form-item label="查询班级" prop="ClassId">
          <el-input  v-model="ClassId"  placeholder="例如：2018级计科-1"
                    prefix-icon='el-icon-view' :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary" @click="submitForm">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-wrapper" >
      <div class="tabel-container">
        <table>
          <thead>
          <tr>
            <th>时间</th>
            <th v-for="(weekNum, weekIndex) in classTableData.courses.length" :key="weekIndex"> {{'周' + digital2Chinese(weekIndex+1, 'week')}}</th>
          </tr>
          </thead>
          <tbody>
          <tr  v-for="(lesson, lessonIndex) in classTableData.lessons" :key="lessonIndex">
            <td>
              <p>{{'第' + digital2Chinese(lessonIndex+1) + "节"}}</p>
              <p class="period">{{ lesson }}</p>
            </td>
            <td  v-for="(course, courseIndex) in classTableData.courses" :key="courseIndex">
              <p>{{classTableData.courses[courseIndex][lessonIndex] || '-'}}</p>
              <p>{{classTableData.teachername[courseIndex][lessonIndex] || '-'}}</p>
              <p>{{classTableData.classroom[courseIndex][lessonIndex] || '-'}}</p>
            </td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>

  </div>
</template>

<script>
import {
  findTimetableByClassId

} from "@/api/timetable";
//  此处请自行替换地址
export default {
  name: "Timetable",
  data() {
    return {
      dialogFormVisible: false,
      ClassId: "",
      type: "",
      multipleSelection: [],formData: {
        teacherName:"",
        courseName:"",
        day:"",
        time:"",
        classroom:"",
        classId:"",

      },


      classTableData: {
        lessons: [
          '08:00-10:00',
          '10:00-12:00',
          '14:30-16:30',
          '16:30-18:30',
          '19:00-21:00'
        ],
        courses: [
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
        ],
        teachername: [
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
        ],
        classroom: [
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
          ["-", "-", "-", "-", "-"],
        ]
      }
    };
  },
  methods: {
    digital2Chinese(num, identifier) {
      const character = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];
      return identifier === 'week' && (num === 0 || num === 7) ? '日' : character[num];
    },
    resettimetable() {
      for (let i=0; i < 5; i++)
      {
        for (let j=0; j < 5; j++)
        {
          this.classTableData.courses[i][j] = '-'
        }
      }
      for (let i=0; i < 5; i++)
      {
        for (let j=0; j < 5; j++)
        {
          this.classTableData.teachername[i][j] = '-'
        }
      }
      for (let i=0; i < 5; i++)
      {
        for (let j=0; j < 5; j++)
        {
          this.classTableData.classroom[i][j] = '-'
        }
      }
    },
    async getTableDataByClassId(ClassId) {
      const res = await findTimetableByClassId({ classId: ClassId });

      if (res.code == 0) {
        this.resettimetable();
        for (let i = 0; i < res.data.retimetable.length; i++)
        { let day = Number(res.data.retimetable[i].day)-1
          let time= Number(res.data.retimetable[i].time)-1
          this.classTableData.courses[day][time] = res.data.retimetable[i].courseName;
          this.classTableData.teachername[day][time] = res.data.retimetable[i].teacherName;
          this.classTableData.classroom[day][time] = res.data.retimetable[i].classroom;
        }
        console.log(this.classTableData.courses)
        this.$forceUpdate();
      }
    },
    submitForm() {
      this.$refs['elForm'].validate(valid => {

        if (!valid) return
        // TODO 提交表单

        this.getTableDataByClassId(this.ClassId)
      })
    },
    resetForm() {
      //this.$refs['elForm'].resetFields()
      this.ClassId = ""
    }
  },
  async created() {
  }
};
</script>

<style lang="scss" scoped>
.class-table {
  .table-wrapper {
    width: 100%;
    height: 100%;
    overflow: auto;
  }
  .tabel-container {
    margin: 7px;

    table {
      table-layout: fixed;
      width: 100%;

      thead {
        background-color: #67a1ff;
        th {
          color: #fff;
          line-height: 30px;
          font-weight: normal;
        }
      }
      tbody {
        background-color: #eaf2ff;
        td {
          color: #677998;
          line-height: 30px;
        }
      }
      th,
      td {
        width: 60px;
        padding: 12px 2px;
        font-size: 12px;
        text-align: center;
      }

      tr td:first-child {
        color: #333;
        .period {
          font-size: 8px;
        }
      }
    }
  }
}
</style>

